//
//  OMPLFeed.m
//  RssRead
//
//  Created by 旺财 on 10-11-2.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "OMPLFeed.h"
#import "Common.h"


@implementation OMPLFeed
@synthesize wcCount;
@synthesize myLoveCount;

- (GDataXMLElement *)getRootNodeFrom:(NSString *)url islocal:(BOOL)islocal recommended:(NSString *)recommendedContent
{
	// load xml file as text
	//NSError *error;
	// *document;
	self.wcCount=0;
	self.myLoveCount=0;
	BOOL needWrite=NO;
	if (islocal)
	{
		NSMutableString *fileText = [[NSMutableString alloc] initWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
		if (recommendedContent!=nil && recommendedContent!=@"-1")		
		{
			NSRange star= [fileText rangeOfString:@"<!--wangcaistar-->" ];
			NSRange end=[fileText rangeOfString:@"<!--wangcaiend-->"];			
			[fileText replaceCharactersInRange:NSMakeRange(star.location+star.length,end.location-(star.location+star.length)) withString:@""];
			[fileText insertString:recommendedContent atIndex:star.location+star.length];
			needWrite=YES;
		}
		//wodezuia
		self.myLoveCount=[[fileText componentsSeparatedByString:@"WoDeZuiAi"] count]-1;
		self.wcCount=[[fileText componentsSeparatedByString:@"WangCaiTuiJian"] count]-1;
		GDataXMLDocument *document = [[GDataXMLDocument alloc]  initWithXMLString:fileText options:0 error:nil];
		[fileText release];
		GDataXMLElement *retval=[[[document rootElement] copy] autorelease];
		//retval=
		[document release];
		if (needWrite)
			[self writefilebyroot:retval];
		return retval;
	}
    else {
		return nil;
		//可能需要 ,from internet 
	}	
    
}

-(NSInteger) getCurarrCountFrom :(GDataXMLElement *) rootNode usexpath: (NSString *) xpath
{
	//NSError* error;
	// get user names by xpath
    NSArray* userList = [rootNode nodesForXPath:xpath error:nil];
	return [userList count];
}

- (NSMutableArray *)getCurArrFrom:(GDataXMLElement *) rootNode usexpath: (NSString *) xpath
{

	NSMutableArray* stories = [[[NSMutableArray alloc] init] autorelease];
	BOOL isnew=FALSE;
	//NSError* error;
    NSArray* userList = [rootNode nodesForXPath:xpath error:nil];
	NSMutableDictionary * item;
	NSString *isad,*sublab,* currentTitle, * currentXmlUrl ,* vtype,* md5filename,*lastupdate,*childcount;//1:opml;2:rss;
	for(GDataXMLElement* node in userList) 
	{
		item = [[NSMutableDictionary alloc] init];
		currentTitle = [[NSString alloc] initWithString:[[node attributeForName:@"title"] stringValue] ];
		currentXmlUrl= [[NSString alloc] init];
		vtype= [[NSString alloc] init];
		md5filename=[[NSString alloc] init];
		childcount=[[NSString alloc] init];
		lastupdate=[[NSString alloc] init];
		sublab=[[NSString alloc] init];
		if ([[[node attributeForName:@"isad"] stringValue] isEqualToString:@"yes"])
			isad=@"yes";
		else {
			isad=@"no";
		}

		if ([[node attributeForName:@"xmlUrl"] stringValue]!=nil)
		{
			vtype=[[NSString alloc] initWithString:@"rss"];
			currentXmlUrl=[[NSString alloc] initWithString:[[node attributeForName:@"xmlUrl"] stringValue]];
			
			if ([[node attributeForName:@"md5filename"] stringValue]==nil) 
			{
				md5filename=[[NSString alloc] initWithFormat: @"%@/mycache/%@.xml",[Common getRootDocDir],[Common md5:currentXmlUrl]];
				[node addAttribute:[GDataXMLNode attributeWithName:@"md5filename" stringValue:md5filename]];
				isnew=YES;
			}
			else 
			{
				md5filename=[[NSString alloc] initWithString:[[node attributeForName:@"md5filename"] stringValue]];
			}

			if ([[node attributeForName:@"notreadcount"] stringValue]!=nil)
			{
				childcount=[[NSString alloc] initWithString:[[node attributeForName:@"notreadcount"] stringValue]];
			}
			else 
			{
				childcount=[[NSMutableString alloc] initWithFormat: @"0"]; 
			}
			if ([[[node attributeForName:@"lastupdate"] stringValue] isEqualToString: @"no" ])
			{
				lastupdate=[[NSString alloc] initWithString:@"no"];
			}
			else 
			{
				lastupdate=[[NSString alloc] initWithString:@"yes"];
			}
				sublab=[[NSString alloc] initWithString:[[node attributeForName:@"description"] stringValue]];
			}
		else 
		{
			vtype= [[NSString alloc] initWithString:@"opml"];
			childcount=[[NSString alloc] initWithFormat: @"%d",[node childCount]]; 
		}
		
		[item setObject:currentTitle forKey:@"title"];
		[item setObject:currentXmlUrl forKey:@"xmlurl"];
		[item setObject:vtype forKey:@"vtype"];
		[item setObject:md5filename forKey:@"md5filename"];
		[item setObject:childcount forKey:@"childcount"];
		[item setObject:lastupdate forKey:@"lastupdate"];
		[item setObject:sublab forKey:@"sublab"];
		[item setObject:isad forKey:@"isad"];
		[stories addObject:item];
		[currentTitle release];
		[currentXmlUrl release];
		[md5filename release];
		[childcount release];
		[lastupdate release];
		[sublab release];
		[isad release];
		[vtype release];
		[item release];
		

	}
		
		if (isnew)
		{
			[self writefilebyroot:rootNode];
		}
		return stories;
	}

- (GDataXMLElement *) WriteNotReadRssCount:(NSInteger)count root:(GDataXMLElement *)rootNode 
								   byXpath:(NSString *) xpath lastupdate: (NSInteger) issusscess 
{
	
	//NSError* error;
	NSArray* userList = [rootNode nodesForXPath:xpath error:nil];
	GDataXMLElement *node=[userList objectAtIndex:0];
	if (issusscess==1 || issusscess==0) 
	{
		NSString *scount=[[NSString alloc] initWithFormat:@"%d",count];
		if ([[node attributeForName:@"notreadcount"] stringValue]==nil) 
		{
			[node addAttribute:[GDataXMLNode attributeWithName:@"notreadcount" stringValue:scount]];
		}
		else 
		{
			[[node attributeForName:@"notreadcount"] setStringValue:scount];
		}
		[scount release];
		if (issusscess==1)
		{
			if ([[node attributeForName:@"lastupdate"] stringValue]==nil) 
			{
				[node addAttribute:[GDataXMLNode attributeWithName:@"lastupdate" stringValue:@"yes"]];
			}
			else 
			{
				[[node attributeForName:@"lastupdate"] setStringValue:@"yes"];
			}
		}
	}
	else  if (issusscess==-1)
	{
		if ([[node attributeForName:@"lastupdate"] stringValue]==nil) 
		{
			[node addAttribute:[GDataXMLNode attributeWithName:@"lastupdate" stringValue:@"no"]];
		}
		else 
		{
			[[node attributeForName:@"lastupdate"] setStringValue:@"no"];
		}
	}
	[self writefilebyroot:rootNode];
	return rootNode;
}
-(void) writefilebyroot:(GDataXMLElement *)rootNode
{
	GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithRootElement:rootNode];
	NSData *xmlData = doc.XMLData;
	NSString *to=[[NSString alloc] initWithFormat:@"%@/op.xml",[Common getRootDocDir]];
	[xmlData writeToFile:to atomically:YES];
	[doc release];
	[to release];
}
- (void)dealloc 
{	
	[super dealloc];
}


@end
